<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function index()
    {
    	$this->login();
    }
    public function login()
    {

    		//商家后台登录
      //判断shopid 是否存在
      if(!empty($_SESSION['yid'])){
        $this->redirect('Item/index');
      }else{
        $this->display('login');
      }
    	
    	
    }
    //验证用户名和密码
    public function verifyUser()
    {
    	// 检测验证码
		    $code=I('post.vercode','','strip_tags');

		    $data['yname']=I('post.username','','strip_tags');
		    $data['password']=md5(I('post.password','','strip_tags'));
		    $data['_logic']='AND';
		    $verify = new \Think\Verify();
		    $result['vercode']=$verify->check($code);		    
		    if(empty($result['vercode'])){
               echo "<script type='text/javascript'>alert('验证码错误请重新填写'); window.history.go(-1);window.location.reload();</script>";
		    }

		    //验证用户名是否存在
		    $result['password']= M('yunying_user')->where("yname='%s'",$data['yname'])->getField('password');
		    if(empty($result['password'])){
                echo "<script type='text/javascript'>alert('用户名不存在'); window.history.go(-1);window.location.reload();</script>";
		    }else{
               $result['yid']=M('yunying_user')->where($data)->getField('yid');
                $result['rank']=M('yunying_user')->where($data)->getField('rank');///获取运营商等级—

               if(empty($result['yid'])){
                    
                    echo "<script type='text/javascript'>alert('密码错误'); window.history.go(-1);window.location.reload();</script>";
               }else{

                 
               	 $_SESSION['yname']=$data['yname'];
               	 $_SESSION['yid']=$result['yid'];
                  $_SESSION['rank']=$result['rank'];///运营商等级
               	// R('Adminlog/addlog',  array(array('登录','后台')));
               	$this->redirect('Item/index');
               }
		    }

    }
    public function logout()
    {

    	if(!empty($_SESSION['yid'])){
            
			$_SESSION=array();
			$this->login();
		}else{
			$this->login();
		}
    }
    //生成验证码
	public function vercode()
	{
		$Verify = new \Think\Verify();
		$Verify->fontSize =35;
		$Verify->length   = 4;
		$Verify->useNoise = false;
        $Verify->entry();
	}

	//空操作
	public function _empty()
	{
		$this->login();
	}
}